package com.zhangyu.shadow.repair.delegate.login;

import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.LoginBean;

import io.reactivex.disposables.Disposable;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/5
 */
public class LoginModel implements LoginContract.Model {

    @Override
    public Disposable provideLoginData(String account,
                                       String password,
                                       String appId,
                                       final IStringDataConvert<LoginBean> convert,
                                       IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_LOGIN)
                .params("workCode", account)
                .params("password", password)
                .params("sourceType", "notSet")
                .params("appId", appId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertLoginBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    private LoginBean convertLoginBean(String response) {
        return new ObjConverter<LoginBean>().convert(response
                , new TypeReference<LoginBean>() {
                });
    }
}
